//
//  NSDictionary+setDic.m
//  FCTools
//
//  Created by WJC on 2018/10/26.
//  Copyright © 2018 wjc. All rights reserved.
//

#import "NSDictionary+setDic.h"
#import <objc/runtime.h>
#import "WJCDefine.h"
@implementation NSDictionary (setDic)

+(void)load{
    static dispatch_once_t onceToken;
    dispatch_once(&onceToken, ^{
        Method old = class_getInstanceMethod(NSClassFromString(@"__NSDictionaryM"), @selector(setObject:forKey:));
        Method new = class_getInstanceMethod(self, @selector(setSaveObject:forKey:));
        if (!old || !new) {
            return;
        }
        method_exchangeImplementations(old, new);
        
        
        Method fromMethod = class_getInstanceMethod(objc_getClass("__NSPlaceholderDictionary"), @selector(initWithObjects:forKeys:count:));
        Method toMethod = class_getInstanceMethod(objc_getClass("__NSPlaceholderDictionary"), @selector(fc_initWithObjects:forKeys:count:));
        method_exchangeImplementations(fromMethod, toMethod);
        
    });
    
}
-(instancetype)fc_initWithObjects:(id *)objects forKeys:(id<NSCopying> *)keys count:(NSUInteger)count {
    @autoreleasepool{
    NSUInteger rightCount = 0;
    for (NSUInteger i = 0; i < count; i++) {
            // 这里只做value 为nil的处理 对key为nil不做处理
        if (objects[i] == nil|| objects[i] == [NSNull null] ) {
            objects[i] = [NSNull null] ; 
        }
        rightCount++;
    }
    return  [self fc_initWithObjects:objects forKeys:keys count:rightCount];
    }
}

-(void)setSaveObject:(id)value forKey:(id)key
{
    @autoreleasepool{
    @try {
        if ([value isKindOfClass:[NSNull class]]||value==nil) {
            return;
        }
        [self setSaveObject:value forKey:key];
    } @catch (NSException *exception) {
        WJCLog(@"nsdic报错,[%@]",exception);
    }
}
    
}
+(id)getDicWithString:(NSString *)str{
    NSError *Returnerror;
    NSData *data=[str dataUsingEncoding:NSUTF8StringEncoding];
    id dic;
    if (str.length>0) {
      dic =[self changeType:[NSJSONSerialization JSONObjectWithData:data options:NSJSONReadingMutableContainers error:&Returnerror]];
    }
    
    if (!Returnerror) {
        return dic;
    }
    else return nil;
}
+(id)getDicWithData:(NSData *)data{
    if ([data isKindOfClass:[NSDictionary class]]||[data isKindOfClass:[NSArray class]]) {
        return data;
    }
    if (data.length>0) {
        if ([data isKindOfClass:[NSString class]]) {
            data = data.mj_JSONData;
        }
    
    NSError *Returnerror;
    id dic=[self changeType:[NSJSONSerialization JSONObjectWithData:data options:NSJSONReadingMutableContainers error:&Returnerror]];
//    NSString *str=[[NSString alloc]initWithData:data encoding:NSUTF8StringEncoding];
    if (!Returnerror) {
        return dic;
    }
        
    }
    return nil;
}
    //主要方法
    //类型识别:将所有的NSNull类型转化成@""
+(id)changeType:(id)myObj
{
    if ([myObj isKindOfClass:[NSDictionary class]])
        {
        return [self nullDic:myObj];
        }
    else if([myObj isKindOfClass:[NSArray class]])
        {
        return [self nullArr:myObj];
        }
    else if([myObj isKindOfClass:[NSString class]])
        {
        return [self stringToString:myObj];
        }
    else if([myObj isKindOfClass:[NSNull class]])
        {
        return [self nullToString];
        }
    else
        {
        return myObj;
        }
}
    //将NSDictionary中的Null类型的项目转化成@""
+(NSDictionary *)nullDic:(NSDictionary *)myDic
{
    NSArray *keyArr = [myDic allKeys];
    NSMutableDictionary *resDic = [[NSMutableDictionary alloc]init];
    @try {
        for (int i = 0; i < keyArr.count; i ++)
        {
        id obj = [myDic objectForKey:keyArr[i]];
        
        obj = [self changeType:obj];
        if (obj) {
            [resDic setObject:obj forKey:keyArr[i]];
        }
        }
    } @catch (NSException *exception) {
        WJCLog(@"%@",exception);
    } @finally {
        return resDic;
    }
    
    
}
    //将NSArray中的Null类型的项目转化成@""
+(NSArray *)nullArr:(NSArray *)myArr
{
    NSMutableArray *resArr = [[NSMutableArray alloc] init];
    for (int i = 0; i < myArr.count; i ++)
        {
        id obj = myArr[i];
        
        obj = [self changeType:obj];
        
        [resArr addObject:obj];
        }
    return resArr;
}

    //将NSString类型的原路返回
+(NSString *)stringToString:(NSString *)string
{
    return string;
}

    //将Null类型的项目转化成@""
+(NSString *)nullToString
{
    return nil;
}
@end
